package com.sgb.kjwl.viewmodel.manage;

import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.ZulinMenuEntity;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.TwoLevelChildForPopWindowEntity;
import com.sgb.kjwl.model.entity.TwoLevelForPopWindowEntity;
import com.sgb.kjwl.model.entity.manage.ManageVideoBean;
import com.sgb.kjwl.model.entity.manage.TheEnterpriseOfAndVideoEntity;
import com.swgk.core.base.model.entity.VideoBean;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.HomeRepertory;
import com.sgb.kjwl.view.ui.fragment.home.EnterpriseManageFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class EnterpriseManageFragModel extends BaseViewModel {
    private EnterpriseManageFragment fragment;
    private HomeRepertory repertory;

    private int pageNum = 1;
    private List<ManageVideoBean> list = new ArrayList<>();
    private List<TheEnterpriseOfAndVideoEntity> list1 = new ArrayList<>();

    public MutableLiveData<List<List<ZulinMenuEntity>>> menuLD = new MutableLiveData<>();

    public EnterpriseManageFragModel(EnterpriseManageFragment fragment, HomeRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    //行业分类
    public void getTradeTypes() {
        APIManager.getInstance().getManageAPI().getCompanyTradeType().enqueue(new Callback<BaseEntity<List<TwoLevelChildForPopWindowEntity<List<TwoLevelForPopWindowEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<TwoLevelChildForPopWindowEntity<List<TwoLevelForPopWindowEntity>>>>> call, Response<BaseEntity<List<TwoLevelChildForPopWindowEntity<List<TwoLevelForPopWindowEntity>>>>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setType(response.body().getData());
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<TwoLevelChildForPopWindowEntity<List<TwoLevelForPopWindowEntity>>>>> call, Throwable t) {
            }
        });
    }

    public void getMenuData() {
        try {
            APIManager.getInstance().getManageAPI().getMenuData().enqueue(new Callback<BaseEntity<List<List<ZulinMenuEntity>>>>() {
                @Override
                public void onResponse(Call<BaseEntity<List<List<ZulinMenuEntity>>>> call, Response<BaseEntity<List<List<ZulinMenuEntity>>>> response) {
                    if (response.body() == null || response.body().getData() == null) {
                        return;
                    }
                    if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                        menuLD.postValue(response.body().getData());
                    } else {
                        MToast.showToast(fragment.getContext(), response.body().getMsg());
                    }
                }

                @Override
                public void onFailure(Call<BaseEntity<List<List<ZulinMenuEntity>>>> call, Throwable t) {
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //获取分类列表
    public void getTypeList() {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getTypeList().enqueue(new Callback<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> call, Response<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<PickSelectEntity<List<PickSelectEntity>>> list = new ArrayList<>();
                    list.add(new PickSelectEntity("", "全部"));
                    list.addAll(response.body().getData());
                    fragment.setTablayout(list);
                } else {
                    MToast.showLongToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity<List<PickSelectEntity>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    //卖家推荐需求
    public void getRecommendedList(boolean isLodMore, Map map) {
        pageNum = isLodMore ? pageNum + 1 : 1;
        map.put("limit", 20);
        map.put("page", pageNum);
        map.put("onMap", "0");
        if (pageNum > 1) {
            DialogHelper.showProgressDialog(fragment.getActivity(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        }
        APIManager.getInstance().getManageAPI().getComList1(map).enqueue(new Callback<BaseEntity<ListResponse<List<TheEnterpriseOfAndVideoEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<TheEnterpriseOfAndVideoEntity>>>> call, Response<BaseEntity<ListResponse<List<TheEnterpriseOfAndVideoEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK) && response.body().getData() != null) {
                    list1 = response.body().getData().getList();
                    getManageVideoList(pageNum, isLodMore);
                } else {
                    MToast.showToast(fragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<TheEnterpriseOfAndVideoEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    //获取距离
    public void getDistanceDatas() {
        List<PickSelectEntity> list = new ArrayList<>();
        PickSelectEntity entity1 = new PickSelectEntity("", "不限");
        PickSelectEntity entity2 = new PickSelectEntity("10", "10KM");
        PickSelectEntity entity3 = new PickSelectEntity("20", "20KM");
        PickSelectEntity entity4 = new PickSelectEntity("30", "30KM");
        PickSelectEntity entity5 = new PickSelectEntity("40", "40KM");
        PickSelectEntity entity6 = new PickSelectEntity("50", "50KM");
        PickSelectEntity entity7 = new PickSelectEntity("60", "60KM");
        PickSelectEntity entity8 = new PickSelectEntity("70", "70KM");
        list.add(entity1);
        list.add(entity2);
        list.add(entity3);
        list.add(entity4);
        list.add(entity5);
        list.add(entity6);
        list.add(entity7);
        list.add(entity8);
        fragment.setDistanceDatas(list);
    }

    //人数筛选数据
    public void getCount() {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getPersonNumber().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<PickSelectEntity> list = response.body().getData();
                    fragment.setCountDatas(list);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getCityData() {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getCityList2().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setCityData(response.body().getData(), repertory.getAreaCode());
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void follow(String shopId) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().userFocusCompany(shopId).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.followSuccess();
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getManageVideoList(int page, boolean isLoadMore) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getManageVideoList(12, page, "企业经营", "网站经营").enqueue(new Callback<BaseEntity<ListResponse<List<VideoBean>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<VideoBean>>>> call, Response<BaseEntity<ListResponse<List<VideoBean>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                List<TheEnterpriseOfAndVideoEntity> videoAndData;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    videoAndData = new ArrayList<>(list1);
                    if (response.body().getData().getList().size() > 0 && list1.size() > 0) {
                        if (list1.size() < 10) {
                            if (response.body().getData().getList().size() < 6) {
                                videoAndData.add(new TheEnterpriseOfAndVideoEntity(response.body().getData().getList()));
                            } else {
                                List<VideoBean> video = new ArrayList<>();
                                for (int i = 0; i < 6; i++) {
                                    video.add(response.body().getData().getList().get(i));
                                }
                                videoAndData.add(new TheEnterpriseOfAndVideoEntity(video));
                            }
                        } else if (list1.size() > 10 && list1.size() < 20) {
                            if (response.body().getData().getList().size() < 6) {
                                videoAndData.add(10, new TheEnterpriseOfAndVideoEntity(response.body().getData().getList()));
                            } else {
                                List<VideoBean> video = new ArrayList<>();
                                for (int j = 0; j < 6; j++) {
                                    video.add(response.body().getData().getList().get(j));
                                }
                                videoAndData.add(10, new TheEnterpriseOfAndVideoEntity(video));
                            }
                        } else {
                            if (response.body().getData().getList().size() < 7) {
                                videoAndData.add(10, new TheEnterpriseOfAndVideoEntity(response.body().getData().getList()));
                            } else {
                                List<VideoBean> video = new ArrayList<>();
                                for (int j = 0; j < 6; j++) {
                                    video.add(response.body().getData().getList().get(j));
                                }
                                videoAndData.add(10, new TheEnterpriseOfAndVideoEntity(video));

                                List<VideoBean> video1 = new ArrayList<>();
                                for (int j = 6; j < response.body().getData().getList().size(); j++) {
                                    video1.add(response.body().getData().getList().get(j));
                                }
                                videoAndData.add(new TheEnterpriseOfAndVideoEntity(video1));
                            }
                        }
                    } else {
                        videoAndData = new ArrayList<>(list1);
                    }
                    fragment.showList(videoAndData, isLoadMore);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<VideoBean>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
